package com.lill.org.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.org.model.CompanyBranchModel;
import com.lill.org.service.ICompanyArchitectureService;
import com.lill.org.service.ICompanyBranchService;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.constant.SystemConstant;
import com.lill.system.model.SysUserEntity;

/**
 * 分公司管理Controller
 *
 * @author lill
 * @date 2019-04-24 15:25:17
 */
@RestController
@RequestMapping("org/branch")
public class CompanyBranchController {
    @Autowired
    private ICompanyBranchService branchService;
    @Autowired
    private ICompanyArchitectureService architectureService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("orgcompanybranch:list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity curUser) {
    	if (!SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
    		params.put("companyId", curUser.getCompanyId());
    	}
        //查询列表数据
        Query query = new Query(params);

        List<CompanyBranchModel> orgCompanyBranchList = branchService.queryList(query);
        int total = branchService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(orgCompanyBranchList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }
   @RequestMapping("/lists")
    //@RequiresPermissions("orgcompanybranch:list")
    public R lists(@RequestParam Map<String, Object> params) {
     /*   if (!SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
            params.put("depart", curUser.getDepartId());
        }*/
        //查询列表数据
        Query query = new Query(params);

        List<CompanyBranchModel> orgCompanyBranchList = branchService.queryLists(query);
        int total = branchService.queryTotals(query);
        PageUtils pageUtil = new PageUtils(orgCompanyBranchList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/businessChange")
    public R businessChange(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity curUser){
        if (!params.containsKey("businessStatus") || !params.containsKey("id")){
            return R.error("请选择正确的门店信息");
        }
        /*Integer result=*/branchService.businessChange(params);
        return R.ok();
    };
    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("orgcompanybranch:info")
    public R info(@PathVariable("id") String id) {
        CompanyBranchModel orgCompanyBranch = branchService.queryObject(id);

        return R.ok().put("orgCompanyBranch", orgCompanyBranch);
    }
/*    @RequestMapping("/infos/{departId}")
    //@RequiresPermissions("orgcompanybranch:info")
    public R infos(@PathVariable("departId") String id) {
        String name = branchService.queryObjects(id);

        return R.ok().put("name", name);
    }*/

    @RequestMapping("/infoAllName/{id}")
    //@RequiresPermissions("orgcompanybranch:info")
    public R infoAllName(@PathVariable("id") String id) {
        CompanyBranchModel orgCompanyBranch = branchService.queryObjectAllName(id);

        return R.ok().put("orgCompanyBranch", orgCompanyBranch);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("orgcompanybranch:save")
    public R save(@RequestBody CompanyBranchModel orgCompanyBranch,@CurrentUser SysUserEntity user) {
    	Map<String, Object> mapQuery=new HashMap<String, Object>();
    	mapQuery.put("serialNo", orgCompanyBranch.getSerialNo());
    	int num=branchService.queryTotal(mapQuery);
    	if(num>0){
    		return R.error(1, "门店编号已存在！");
    	}
    	
    	if(StringUtils.isEmpty(orgCompanyBranch.getCompanyId())){
    		orgCompanyBranch.setCompanyId(user.getCompanyId());
    	}
    	orgCompanyBranch.setStatus(1);
    	orgCompanyBranch.setCreateUser(user.getId());
    	orgCompanyBranch.setCreateTime(new Date());
        int result=branchService.save(orgCompanyBranch);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("orgcompanybranch:update")
    public R update(@RequestBody CompanyBranchModel orgCompanyBranch,@CurrentUser SysUserEntity user) {
    	Map<String, Object> mapQuery=new HashMap<String, Object>();
    	mapQuery.put("serialNo", orgCompanyBranch.getSerialNo());
    	mapQuery.put("idNq", orgCompanyBranch.getId());
    	int num=branchService.queryTotal(mapQuery);
    	if(num>0){
    		return R.error(1, "门店编号已存在！");
    	}
    	orgCompanyBranch.setUpdateUser(user.getId());
    	orgCompanyBranch.setUpdateTime(new Date());
        int result=branchService.update(orgCompanyBranch);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("orgcompanybranch:delete")
    public R delete(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
    	
        int result=architectureService.deleteLogicByMap(params);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity curUser) {
    	if (!SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
    		params.put("companyId", curUser.getCompanyId());
    	}
        List<CompanyBranchModel> list = branchService.queryList(params);

        return R.ok().put("list", list);
    }
}
